package cn.yunhe.spring.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2016/12/11.
 */
@Service
public class RedisCacheUtil<T> {
    @Autowired
    @Qualifier("jedisTemplate")
    public RedisTemplate redisTemplate;

    /**
     * 缓存基本的对象，Integer,String,实体类等
     * @param key 缓存的键值
     * @param value 缓存的值
     * @return 缓存的对象
     * */
    public <T>ValueOperations<String,T> setCacheObject(String key,T value){
        ValueOperations<String,T> operations=redisTemplate.opsForValue();
        operations.set(key,value);
        return operations;
    }

    /**
     * 获得缓存的基本对象
     * @param key 缓存的值
     * @param operations
     * @return 缓存键值对应的值
     * */
    public <T> T getCacheObject(String key,ValueOperations<String,T> operations){
        ValueOperations<String,T> operations1=redisTemplate.opsForValue();
        return operations.get(key);
    }

    /**
     * 缓存list数据
     * @param key 缓存的键值
     * @param dataList 待缓存的List数据
     * @return 缓存的对象
     * */
    public <T>ListOperations<String,T> setCacheList(String key, List<T> dataList){
        ListOperations listOperations=redisTemplate.opsForList();
        if(null!=dataList){
            int size=dataList.size();
            for(int i=0;i<size;i++){
                listOperations.rightPush(key,dataList.get(i));
            }
        }
        return listOperations;
    }

    /**
     * 缓存set
     * @param key 缓存的键值
     * @param dataSet 缓存的数据
     * @return 缓存的数据
     * */
    public <T>BoundSetOperations<String,T> setCacheSet(String key,Set<T> dataSet){
        BoundSetOperations<String,T> setOperations=redisTemplate.boundSetOps(key);
        Iterator<T> it=dataSet.iterator();
        while(it.hasNext()){
            setOperations.add(it.next());
        }
        return setOperations;
    }

    /**
     * 获得缓存的Set
     * @param key
     * @param operations
     * @return
     * */
    public Set<T> getCacheSet(String key,BoundSetOperations<String,T> operations){
        Set<T> dataSet=new HashSet<T>();
        BoundSetOperations<String,T> operations1=redisTemplate.boundSetOps(key);
        Long size=operations1.size();
        for(int i=0;i<size;i++){
            dataSet.add(operations1.pop());
        }
        return dataSet;
    }

    /**
     * 缓存Map
     * @param key
     * @param dataMap
     * @return
     * */
    public <T> HashOperations<String,String,T> setCacheMap(String key,Map<String,T> dataMap){
        HashOperations hashOperations=redisTemplate.opsForHash();
        if(null!=dataMap){
            for(Map.Entry<String,T> entry:dataMap.entrySet()){
                hashOperations.put(key,entry.getKey(),entry.getValue());
            }
        }
        return hashOperations;
    }

    /**
     * 获得缓存的Map
     * @param key
     * @param hashOperations
     * @return
     * */
    public <T> Map<String,T> getCacheMap(String key,HashOperations<String,String,T> hashOperations){
        Map<String,T> map=redisTemplate.opsForHash().entries(key);
        return map;
    }

    /**
     * 缓存Map
     * @param key
     * @param dataMap
     * */
    public <T>HashOperations<String,Integer,T> setCacheIntegerMap(String key, Map<Integer, T> dataMap) {
        HashOperations hashOperations=redisTemplate.opsForHash();
        if(null!=dataMap){
            for(Map.Entry<Integer,T> entry:dataMap.entrySet()){
                hashOperations.put(key,entry.getKey(),entry.getValue());
            }
        }
        return hashOperations;

    }

    /**
     * 获得缓存的Map
     * @param key
     * @param hashOperations
     * @return
     * */
    public <T> Map<Integer,T> getCacheIntegerMap(String key,HashOperations<String,String,T> hashOperations){
        Map<Integer,T> map=redisTemplate.opsForHash().entries(key);
        return map;
    }
}
